// Decompiled with JetBrains decompiler
// Type: FibMatrix.Config.MetaItemSet
// Assembly: FMCommon, Version=1.0.8663.21262, Culture=neutral, PublicKeyToken=null
// MVID: 52773074-FB09-4BA0-8FA9-4782D11D6C17
// Assembly location: /Users/mac/work/RiverGame/com.rivergame.fmframework/Runtime/Plugins/FMCommon.dll

using System;
using System.Collections;
using System.Collections.Generic;

namespace FibMatrix.Config
{
  public abstract class MetaItemSet : IMetaItemSet, IEnumerator<IMetaKeyValueCollection>, IDisposable, IEnumerator
  {
    protected string _name;
    protected MetaKeyValueCollection _kvMap;
    protected MetaItemHelper _helper;
    private int _iterRow;
    private MetaKeyValueCollection _current;

    public string name
    {
      get
      {
        return this._name;
      }
    }

    public int propNum
    {
      get
      {
        return this._helper.propNum;
      }
    }

    public int totalCount
    {
      get
      {
        return this._helper.totalNum;
      }
    }

    public byte[] buffer
    {
      get
      {
        return this._helper.buffer;
      }
    }

    public int propAreaLength
    {
      get
      {
        return this._helper.propAreaLength;
      }
    }

    public int propRowPosStartOffset
    {
      get
      {
        return this._helper.propRowPosStartOffset;
      }
    }

    public int propRowAreaStartOffset
    {
      get
      {
        return this._helper.propRowAreaStartOffset;
      }
    }

    public MetaIdType idType
    {
      get
      {
        return this._helper.idType;
      }
    }

    public MetaItemSet(string name)
    {
      this._name = name;
    }

    public void Initialize(
      MetaItemHelper helper,
      string[] uniqueStrs,
      int[] uniqueInts,
      long[] uniqueLongs,
      float[] uniqueFloats,
      double[] uniqueDouble,
      bool delayMode = false,
      bool logPerformanceWarning = true)
    {
      this._helper = helper;
      this._kvMap = new MetaKeyValueCollection(this._name, this._helper, uniqueStrs, uniqueInts, uniqueLongs, uniqueFloats, uniqueDouble, logPerformanceWarning);
      this._kvMap.InitializePropNames();
      if (!delayMode)
        return;
      this.CalculateIdRowIndexMap();
    }

    protected abstract void CalculateIdRowIndexMap();

    public bool TryGetPropNameId(string propName, out int id)
    {
      id = this.PropName2Id(propName);
      return id >= 0;
    }

    public int PropName2Id(string propName)
    {
      return this._kvMap.Key2Id(propName);
    }

    public Dictionary<string, MetaSerializeType> GetAllPropertyNameAndType()
    {
      int propNum = this._helper.propNum;
      Dictionary<string, MetaSerializeType> dictionary = new Dictionary<string, MetaSerializeType>(propNum);
      for (int index = 0; index < propNum; ++index)
      {
        int propNameAt = this._helper.GetPropNameAt(index);
        MetaSerializeType propTypeAt = this._helper.GetPropTypeAt(index);
        string stringByIndex = this._kvMap.GetStringByIndex(propNameAt);
        dictionary.Add(stringByIndex, propTypeAt);
      }
      return dictionary;
    }

    public IMetaKeyValueCollection Current
    {
      get
      {
        return (IMetaKeyValueCollection) this._current;
      }
    }

    object IEnumerator.Current
    {
      get
      {
        return (object) this._current;
      }
    }

    public void Dispose()
    {
      if (this._kvMap != null)
      {
        this._kvMap.Dispose();
        this._kvMap = (MetaKeyValueCollection) null;
      }
      if (this._helper != null)
      {
        this._helper.Dispose();
        this._helper = (MetaItemHelper) null;
      }
      this._current = (MetaKeyValueCollection) null;
    }

    public bool MoveNext()
    {
      if (this._iterRow < this.totalCount)
      {
        this._current = this._kvMap;
        this._kvMap.FillRowVals();
        ++this._iterRow;
        if (this._iterRow < this.totalCount)
          this._helper.MoveToRow(this._iterRow);
        return true;
      }
      this._current = (MetaKeyValueCollection) null;
      return false;
    }

    public void Reset()
    {
      this._iterRow = 0;
      this._helper.MoveToRow(0);
      this._current = (MetaKeyValueCollection) null;
    }

    public unsafe Dictionary<string, (int, int)> CalculateRowPosAndLengthMap(
      out List<string> ids)
    {
      int totalCount = this.totalCount;
      ids = new List<string>(totalCount);
      Dictionary<string, (int, int)> dictionary = new Dictionary<string, (int, int)>(totalCount);
      int currentRow = this._helper.currentRow;
      fixed (byte* bufferPtr = this._helper.buffer)
      {
        for (int row = 0; row < totalCount; ++row)
        {
          this._helper.MoveToRow(row);
          string key = (string) null;
          switch (this._helper.GetFirstType(bufferPtr))
          {
            case MetaSerializeType.Int:
              int val1;
              if (this._helper.GetFirstPropVal(bufferPtr, MetaSerializeType.Int, out val1))
              {
                key = val1.ToString();
                break;
              }
              break;
            case MetaSerializeType.Long:
              int val2;
              if (this._helper.GetFirstPropVal(bufferPtr, MetaSerializeType.Long, out val2))
              {
                key = this._kvMap.GetLongByIndex(val2).ToString();
                break;
              }
              break;
            case MetaSerializeType.String:
              int val3;
              if (this._helper.GetFirstPropVal(bufferPtr, MetaSerializeType.String, out val3))
              {
                key = this._kvMap.GetStringByIndex(val3);
                break;
              }
              break;
          }
          if (key == null)
            throw new Exception("id is null");
          ids.Add(key);
          dictionary.Add(key, (this._helper.rowStartPos, (row < totalCount - 1 ? this._helper.GetRowStartOffsetAt(row + 1) : this._helper.propAreaLength + this._helper.propRowAreaStartOffset) - this._helper.rowStartPos));
        }
      }
      this._helper.MoveToRow(currentRow);
      return dictionary;
    }
  }
}
